﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System.Collections.Generic;
using W.Purchase.API.Application.Command.ClientCommand;

namespace W.Purchase.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class UnitController : ControllerBase
    {
        private readonly IMediator mediator;
        public UnitController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加单位信息
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<ApiResult<int>> AddAsync(UnitAddCommand unit)
        {
            ApiResult<int> list = new ApiResult<int>();
            try
            {
                list.Code = await mediator.Send(unit);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 删除单位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelectUnit(UnitDeleteCommand command)
        {
            ApiResult<int> list= new ApiResult<int>();
            try
            {
                list.Code= await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 反填单位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Domain.Unit>> FindUnit([FromQuery] UnitFindCommand command)
        {
            ApiResult<Domain.Unit> list= new ApiResult<Domain.Unit>();
            try
            {
                list.Data=await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 修改单位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AllDelectUnit(UnitUpdateCommand command)
        {
            ApiResult<int> list= new ApiResult<int>();
            try
            {
                list.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        /// <summary>
        /// 批量删除单位信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Handle(UnitAllDeleteCommand command)
        {
            ApiResult<int> list= new ApiResult<int>();
            try
            {
                list.Data = await mediator.Send(command);

            }
            catch (Exception ex)
            {
                list.Code = -1;
                list.Message = ex.Message;
                throw;
            }
            return list;
        }
        

    }
}
